package com.shaoshaohuo.app.ui;

import java.util.ArrayList;
import java.util.List;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.Message;
import com.shaoshaohuo.app.entity.MessageListEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.adapter.MessageListAdapter;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.shaoshaohuo.app.view.XListView.IXListViewListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
* @类   说   明:	
* @version 1.0
* @创建时间：2015年8月6日 下午2:08:17
* 
*/
public class MessageCenterActivity extends BaseActivity implements OnClickListener, IXListViewListener {
    
    private static final int TYPE_MESSAGE = 1;
    private static final int TYPE_ACTIVITY= 2;
    
    private TopbarView mTopbarView;

    private XListView mListview;
    
    private TextView mMessageText;
    private TextView mActivityText;

    private MessageListAdapter<Message> mMessageAdapter;
    
    private List<Message> mDataList = new ArrayList<Message>();
    private int size = 10;
    private String action = ListViewAction.NEW;
    private String cursor = "0";
    private int type = TYPE_MESSAGE;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_center);
        initView();
        setUpView();
        getList(true);
    }

    private void initView() {
        mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
        
        mMessageText = (TextView) this.findViewById(R.id.textview_message1);
        mActivityText = (TextView) this.findViewById(R.id.textview_activity);
        
        mListview = (XListView) this.findViewById(R.id.listview_data_list);
    }

    private void setUpView() {
        mTopbarView.setCenterText("我的消息");
        mTopbarView.setLeftView(true, true);

        
        mMessageText.setOnClickListener(this);
        mActivityText.setOnClickListener(this);
     
        mMessageText.setSelected(true);
        mActivityText.setSelected(false);
        
        setListView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.textview_message1:
            if(mMessageText.isSelected()){
                return;
            }
            mMessageText.setSelected(true);
            mActivityText.setSelected(false);
            
            changeStatus(TYPE_MESSAGE);
            break;
        case R.id.textview_activity:
            if(mActivityText.isSelected()){
                return;
            }
            mMessageText.setSelected(false);
            mActivityText.setSelected(true);
            
            changeStatus(TYPE_ACTIVITY);
            break;
        }
    }
    
    private void getList(boolean isLoading){
        if(isLoading){
            startLoadingDialog();
        }
        RequestService.getInstance().getMessageList(this, size+"", cursor, action, type+"", MessageListEntity.class, new RequestListener() {
            
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if(resultData.isOk()){
                    MessageListEntity entity = (MessageListEntity) resultData;
                    List<Message> list = entity.getData().getList();
                    mDataList.addAll(list);
                    
                    if ((mDataList.size() < size) || (action.equals(ListViewAction.HISTORY) && list.size() < size)) {
//                      mListview.setPullLoadEnable(false);
                        mListview.endLoadingMore("没有更多了");
                    }
                    setAdapter(mDataList);
                }else{
                    showToast(resultData.getMsg());
                }
//                onLoad();
            }
            
            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(msg);
                onLoad();
            }
        });
    }
    
    protected void setAdapter(List<Message> mDataList2) {
        
        if(mMessageAdapter == null){
            mMessageAdapter = new MessageListAdapter<Message>(this, mDataList2, false);
            mMessageAdapter.setMessage(type == TYPE_MESSAGE);
            mListview.setAdapter(mMessageAdapter);
        }else{
            mMessageAdapter.setMessage(type == TYPE_MESSAGE);
            mMessageAdapter.setData(mDataList2);
        }
    }

    private void setListView() {
        mListview.setPullLoadEnable(true);
        mListview.setPullRefreshEnable(true);
        mListview.setXListViewListener(this);
        
        mListview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Message msg = (Message) parent.getItemAtPosition(position);
                if(type == TYPE_MESSAGE){
                    
                }else{
                    Intent aboutIntent = new Intent(MessageCenterActivity.this,WebBrowserActivity.class);
                    aboutIntent.putExtra(WebBrowserActivity.TITLE, msg.getTitle());
                    aboutIntent.putExtra(WebBrowserActivity.URL, msg.getUrl());
                    startActivity(aboutIntent);
                }
            }
        });
    }
    
    private void changeStatus(int status) {
        type = status;
        onRefresh();
    }
    
    @Override
    public void onRefresh() {
        cursor = "0";
        mDataList.clear();
        action = ListViewAction.NEW;
        mListview.setPullLoadEnable(true);
        getList(false);
    }

    @Override
    public void onLoadMore() {
        if(mDataList.size()>0){
            cursor = mDataList.get(mDataList.size()-1).getCursor();
        }else {
            cursor = "0";
        }
        action = ListViewAction.HISTORY;
        getList(false);
    }

    private void onLoad() {
        mListview.stopRefresh();
        mListview.stopLoadMore();
//      mListview.setRefreshTime(StringHelper.formatDate(System.currentTimeMillis()+""));
    }
    
}
